﻿using System.Collections.Generic;

namespace Core.Res
{
    public enum ELoadState
    {
        Invalid,
        Loading,
        Complete
    }

    public abstract class LoadAwaiter : ILoadProgress
    {
        private readonly ILoader loader;

        public ETTaskAwaiter Awaiter { get; protected set; }
        public void CreateAwaiter()
        {
            Awaiter = new();
        }

        public ELoadState State { get; protected set; }
        public float Progress { get; protected set; }
        public string Name { get; protected set; }

        protected LoadAwaiter(ILoader loader)
        {
            this.loader = loader;
            Rely = new HashSet<string>();
        }


        public HashSet<string> Rely { get; protected set; }
        private List<string> mTempList = new();

        public void RefreshRely()
        {
            if (Rely.Count <= 0) return;

            mTempList.Clear();
            foreach (var s in Rely)
            {
                if (loader.CheckAsset(s))
                {
                    mTempList.Add(s);
                }
            }
            foreach (var item in mTempList)
            {
                Rely.Remove(item);
            }
            mTempList.Clear();
        }


        public abstract void Start();
        public abstract void Update();
        public abstract void Finish();

    }
}
